package com.backend.controller;

import com.backend.anno.Log;
import com.backend.entity.*;
import com.backend.service.WebService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;
import java.util.List;

@RestController
@RequestMapping("/flow")
@Slf4j
public class WebController {
    @Autowired
    private WebService webService;

    //查看全部信息
    @Log
    @GetMapping("/listAllFlow")
    public Result<PageBean> getAllFlows(
            @RequestParam Integer pageNumber,
            @RequestParam Integer pageSize
    ){
        PageBean pageBean = new PageBean();
        List<CarFlow> list = webService.listAll(pageNumber,pageSize);
        pageBean.setItems(list);
        pageBean.setCurrentPage(pageNumber);
        pageBean.setPages((int) (webService.listAllNum()/pageSize));
        pageBean.setTotal(webService.listAllNum());
        return Result.success(pageBean);
    }

    //查看全部警报
    @Log
    @GetMapping("/listAllWarn")
    public Result<PageBean> getAllWarn(
            @RequestParam Integer pageNumber,
            @RequestParam Integer pageSize
    ){
        PageBean pageBean = new PageBean();
        List<Warn> list = webService.listAllWarn(pageNumber,pageSize);
        pageBean.setItems(list);
        pageBean.setCurrentPage(pageNumber);
        pageBean.setPages((int) (webService.listAllWarnNum()/pageSize));
        pageBean.setTotal(webService.listAllWarnNum());
        return Result.success(pageBean);
    }

    //查看全部车辆
    @Log
    @GetMapping("/listAllVehicle")
    public Result<PageBean> getAllVehicle(
            @RequestParam Integer pageNumber,
            @RequestParam Integer pageSize
    ){
        PageBean pageBean = new PageBean();
        List<Vehicle> list = webService.listAllVehicle(pageNumber,pageSize);
        pageBean.setItems(list);
        pageBean.setCurrentPage(pageNumber);
        pageBean.setPages((int) (webService.listAllVehicleNum()/pageSize));
        pageBean.setTotal(webService.listAllVehicleNum());
        return Result.success(pageBean);
    }

    //筛选信息
    @Log
    @GetMapping("/listFlowByTimeAndSpot")
    public Result<PageBean> getFlowByTimeAndSpot(
            @RequestParam(required = false)LocalDateTime beginTime,
            @RequestParam(required = false)LocalDateTime endTime,
            @RequestParam("spot") String spot,
            @RequestParam Integer pageNumber,
            @RequestParam Integer pageSize
    ){
        PageBean pageBean = new PageBean();
        List<CarFlow> list = webService.listByTimeAndSpot(beginTime,endTime,spot,pageNumber,pageSize);
        pageBean.setItems(list);
        pageBean.setCurrentPage(pageNumber);
        pageBean.setPages((int) (webService.listByTimeAndSpotNum(beginTime,endTime,spot)/pageSize));
        pageBean.setTotal(webService.listByTimeAndSpotNum(beginTime,endTime,spot));
        return Result.success(pageBean);
    }
}
